﻿
(function ($) {
    $.extend({
        Loading: {
            visibled: false,
            defaults: {
                theme: '',
                modal: 1,
                zIndex: 99999,
                position: ['center', 'center'], // ['center','top','right','bottom','left']
                margin: 10
            },
            undefined: function () {
                for (var i = 0; i < arguments.length; i++)
                    if (typeof arguments[i] != 'undefined') return false;
                return true;
            },
            viewport: function () {
                var d = document.documentElement, b = document.body, w = window;
                return jQuery.extend(
                    jQuery.browser.msie ?
                        { left: b.scrollLeft || d.scrollLeft, top: b.scrollTop || d.scrollTop} :
                        { left: w.pageXOffset, top: w.pageYOffset },
                    !this.undefined(w.innerWidth) ?
                        { width: w.innerWidth, height: w.innerHeight} :
                        (!this.undefined(d) && !this.undefined(d.clientWidth) && d.clientWidth != 0 ?
                            { width: d.clientWidth, height: d.clientHeight} :
                            { width: b.clientWidth, height: b.clientHeight }));
            },
            format: function () {
                var result = arguments[0];
                for (var i = 1; i < arguments.length; i++) {
                    result = result.replace(new RegExp('\\{' + (i - 1) + '\\}', 'g'), arguments[i]);
                }
                return result;
            },
            getId: function () {
                return $(this.format("#loading_{0}", this.Id));
            },
            showPosition: function () {

                if (!this.visibled) return;

                var s = this.options;
                var e = this.getId();
                var v = this.viewport();
                var h = e.outerHeight();
                var w = e.outerWidth();

                var t = 0, l = 0;

                switch (s.position[0]) {

                    default:
                    case 'center':
                        l = v.left + ((v.width - w) / 2);
                        t = v.top + ((v.height - h) / 2);
                        break;
                    case 'top-left':
                        t = v.top + s.margin;
                        l = v.left + s.margin;
                        break;
                    case 'top-right':
                        t = v.top + s.margin;
                        l = v.left + v.width - w - s.margin * 2;
                        break;

                    case 'bottom-left':
                        t = v.top + v.height - h - s.margin;
                        l = v.left + s.margin;
                        break;
                    case 'bottom-right':
                        t = v.top + v.height - h - s.margin;
                        l = v.left + v.width - w - s.margin * 2;
                        break;

                }
                if (this.animating) {
                    this.animating.stop();
                }
                e.show();
                this.animating = e.animate({ top: t, left: l }, {
                    duration: this.options.showDuration,
                    queue: false
                });

            },
            setEvents: function () {
                $(window).bind('resize scroll', $.proxy(function () {

                    this.showPosition();

                }, this));

                $(window).bind('keydown', $.proxy(function (e) {
                    if (e.keyCode == 27) {
                        this.hide();
                        e.preventDefault();
                    }
                }, this));
            },
            create: function (s) {

                this.Id = new Date().getTime();

                var e = $(this.format('<div id="loading_{0}" class="loading {1}"></div>', this.Id, s.theme));

                $('body').append(e);
                e.css({ position: 'absolute', zIndex: s.zIndex });

                this.showPosition();
                this.setEvents();

                //this.created = true;
            },

            show: function (s, o) {


                this.options = s = $.extend({}, this.defaults, s || {});
                if (this.options.modal) {
                    o = $.extend(true, { zIndex: s.zIndex - 1 }, o || {});
                    $.Overlay.show(o);
                }
                this.visibled = true;
                if (this.Id) {
                    this.showPosition();
                }
                else {

                    this.create(s);
                }

            },
            hide: function () {
                if (this.options.modal) {
                    $.Overlay.hide();
                }
                this.getId().hide();
                this.visibled = false;
            }
        }
    });

})(jQuery);



